package com.organ.base.service;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class LoginService implements UserDetailsService {
	
	
	public HttpSession session;
	
	public SysMemberService memberService;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		//根据用户名查找密码
		String pwd = memberService.findpwdByMemberNumber(username);
		List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
		getRoles(list);
		// 把账号密码放置user中
		User auth_user = new User(username,pwd,list);
		return auth_user;
	}

	/*
	 * 获取所属角色
	 * @param list
	 */
	public void getRoles(List<GrantedAuthority> list) {
		for (String role : "USER,ADMIN,".split(",")) {
			list.add(new SimpleGrantedAuthority("ROLE_" + role));
			//权限如果前缀是ROLE_，security就会认为这是个角色信息，而不是权限，例如ROLE_MENBER就是MENBER角色，CAN_SEND就是CAN_SEND权限
		}
	}
}
